package com.test.ServletUpload;

import com.test.jdbc.JDBCUtils2;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ModelSearch",value = "/ModelSearch")
public class ModelSearch extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        List<Object[]> modellist = null;
        try {
            modellist = queryRunner.query("show tables",new ArrayListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ArrayList<String> modelTable = new ArrayList<String>();
        for(Object[] objects : modellist) {
            StringBuilder sb = new StringBuilder();
            for(Object obj : objects){
                String s = obj.toString();
                sb.append(s);
            }
            String ss = sb.toString();
            String check = ss.substring(ss.length() - 6, ss.length());
            if(check.equals("_model")){
                modelTable.add(ss);
            }
        }

        request.getSession().setAttribute("modelTable", modelTable);

        response.sendRedirect("/Java_web/uploadModel.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
